<?php
/**
 * An HTML assistant class that takes care of the nitty gritty and commonly used
 * features of HTML. Add to this class if needed.
 * @author Marlon Brandon van der Linde
 * @copyright Marlon B van der Linde 2008
 * @version 1.00
 * @license http://www.opensource.org/licenses/mit-license.php MIT-License
*/

class MoltenBlobHTML {
	
	private $pageContent;
	protected $pageTitle, $pageCSS, $description, $keywords;
	const version = '0.00';

	/**
	 * Constructor
	 */
	function __construct($title='', $css='', $description='', $keywords='') {
		$this->pageContent = NULL;
		$this->configure($title, $css, $description, $keywords);
		$this->htmlHeader();		// header FIRST!
	}

	/**
	 * Destructor
	 */
	function __destruct() {
	}


	/**
	 * Configures the pahe title, stylesheet, descriptions and keywords
	 * Use or adapt this function to handle everything <head> related
	 * @param string $title Title of the page, same as <title>
	 * @param string $css The full path to the stylesheet file
	 * @param string $description The page description
	 * @param string $keywords A string containing the keywords meta value
	 */
	protected function configure($title, $css, $description, $keywords) {
		#TODO: validation
		$this->pageTitle = $title;
		$this->pageCSS = $css;
		$this->description = $description;
		$this->keywords = $keywords;
	}


	/**
	 * Renders/Prints whatever content is stored in pageContent to the screen
	 * after performing cleanup tasks etc on it
	 */
	function renderPage() {

		$this->htmlFooter();	// Footer LAST!

		if(function_exists('tidy_parse_string')) {
			$this->pageContent .= "<!-- Using PHP TIDY -->";
			$config = array(
				'indent' => true,
				'output-xhtml' => true,
				'wrap' => 150,
				'indent' => true,
				'indent-spaces' => 4,
				);

			$tidy = tidy_parse_string($this->pageContent, $config, 'UTF8');
			$tidy->cleanRepair();

			echo($tidy);
		} else {
			echo($this->pageContent);
		}
	}


	/**
	 * Add the <head> tags as configured, to the pageContent variable
	 * @access protected
	 * @see configure()
	 */
	protected function htmlHeader() {
		$this->pageContent .= "<html><head>";
		$this->pageContent .= "<title>$this->pageTitle</title>";
		$this->pageContent .= "<link href=$this->pageCSS rel='stylesheet' type='text/css' />";
		$this->pageContent .= "<meta http-equiv='content-type' content='text/html; charset=utf-8'>";
		$this->pageContent .= "<meta http-equiv='content-language' content='en'>";
		$this->pageContent .= "<meta name='description' content='$this->description'>";
		$this->pageContent .= "<meta name='keywords' content='$this->keywords'>";
		$this->pageContent .= "</head><body>";
	}


	/**
	 * Add the footer html into the pageContent variable
	 * @access protected
	 */
	protected function htmlFooter() {
		$this->pageContent .= "<!-- Page generated by MoltenBlob/HTMLTools version ".self::version." - Marlon van der Linde -->";
		$this->pageContent .= "</body>";
		$this->pageContent .= "</html>";
	}

	
	/**
	 * Build an ordered or unordered list from the data supplied in an array
	 * @param array $data A single dimensional array containing the strings to create a list from
	 * @param bool $ordered Build an ordered list or not by passing true or false to this parameter
	 */
	function buildList($data, $ordered=false) {
	
		$out = NULL;
		
		if($ordered) { 
			$out .= "<ol>";
		} else {
			$out .= "<ul>";
		}
		
		foreach($data as $item) {
			 $out .= "<li>$item</li>";
		}
		if($ordered) {
			$out .= "</ol>";
		} else {
			$out .= "</ul>";
		}
	
		$this->pageContent .= $out;
	}


	/**
	 * Open a div tag for a specific class, and stored it inside pageContent
	 * @param string $class The class to hook this div to
	 */
	function divOpen($class) {
		$this->pageContent .= "<div class='$class'>";
	}


	/**
	 * Close the div tags in the crudest, quickest way
	 * @todo Major improvements on how this is done
	 */
	function divClose() {
		$this->pageContent .= "</div>";
	}


	/**
	 * Build a list format menu from the supplied data, and stored the html inside pageContent
	 * @todo href handing inside the array
	 * @param string $title The title of this menu instance
	 * @param array Ab array of data
	 * @param string $class The css class to use for this menu
	 */
	function menu($title, $data, $class) {
		$o .= "<div class='$class'>";
		$o .= "<h2>$title</h2><ul>";
		foreach($data as $item) {
			$o .= "<li>$item</li>";
		}
		$o .= "</ul></div>";
		$this->pageContent .= $o;
	}


	// dump function to dump any content
	function pushContent($stuff) {
		$this->pageContent .= $stuff;
	}
	

}



?>
